﻿using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
using D.UtilCore.Dtos;

namespace CarAppClient.Controls.MessageUsercontrols
{
    public class MsgUtil
    {
        public static MessageBoxResult ShowMsg(StatusCodeEnum statusCodeEnum, string? successMsg = null, string? errorMsg = null)
        {
            successMsg = successMsg ?? string.Empty;
            errorMsg = errorMsg ?? string.Empty;
            //  判断结果
            switch (statusCodeEnum)
            {
                case StatusCodeEnum.OK:
                    return Info(successMsg);
                case StatusCodeEnum.Waring:
                case StatusCodeEnum.Params:
                    return Warning(errorMsg);
                case StatusCodeEnum.Error:
                    return Error(errorMsg);
                case StatusCodeEnum.BadRequest:
                    return Error("请求有异常信息，服务器拒绝处理！");
                case StatusCodeEnum.Unauthorized:
                    return Error("暂未授权！");
                case StatusCodeEnum.Forbidden:
                    return Error("服务端拒绝处理该消息！");
                case StatusCodeEnum.NotFound:
                    return Error("请求地址不存在！");
                case StatusCodeEnum.MenthodNotAllowed:
                    return Error("不允许请求该地址！");
                case StatusCodeEnum.ServerError:
                    return Error("服务器异常！");
                default:
                    return Error("客户端异常！");
            }
        }

        /// <summary>
        /// 错误信息
        /// </summary>
        /// <param name="messageBoxText"></param>
        /// <param name="button"></param>
        /// <param name="caption"></param>
        /// <param name="icon"></param>
        /// <returns></returns>
        public static MessageBoxResult Error(string? messageBoxText, MessageBoxButton button = MessageBoxButton.YesNoCancel, string caption = "错误", MessageBoxImage icon = MessageBoxImage.Error)
        {
            messageBoxText = messageBoxText ?? string.Empty;
            var msg = new WPFMessageBox(messageBoxText, caption, button, icon);
            return GetWindow(msg);
        }

        /// <summary>
        /// 信息提示
        /// </summary>
        /// <param name="messageBoxText"></param>
        /// <param name="button"></param>
        /// <param name="caption"></param>
        /// <param name="icon"></param>
        /// <returns></returns>
        public static MessageBoxResult Info(string? messageBoxText, MessageBoxButton button = MessageBoxButton.YesNoCancel, string caption = "提示", MessageBoxImage icon = MessageBoxImage.Information)
        {
            messageBoxText = messageBoxText ?? string.Empty;
            var msg = new WPFMessageBox(messageBoxText, caption, button, icon);
            return GetWindow(msg);
        }

        /// <summary>
        /// 真香警告
        /// </summary>
        /// <param name="messageBoxText"></param>
        /// <param name="button"></param>
        /// <param name="caption"></param>
        /// <param name="icon"></param>
        /// <returns></returns>
        public static MessageBoxResult Warning(string? messageBoxText, MessageBoxButton button = MessageBoxButton.YesNoCancel, string caption = "警告", MessageBoxImage icon = MessageBoxImage.Warning)
        {
            messageBoxText = messageBoxText ?? string.Empty;
            var msg = new WPFMessageBox(messageBoxText, caption, button, icon);
            return GetWindow(msg);
        }

        /// <summary>
        /// 询问信息
        /// </summary>
        /// <param name="messageBoxText"></param>
        /// <param name="button"></param>
        /// <param name="caption"></param>
        /// <param name="icon"></param>
        /// <returns></returns>
        public static MessageBoxResult Question(string messageBoxText, MessageBoxButton button = MessageBoxButton.YesNo, string caption = "询问", MessageBoxImage icon = MessageBoxImage.Question)
        {
            messageBoxText = messageBoxText ?? string.Empty;
            var msg = new WPFMessageBox(messageBoxText, caption, button, icon);
            return GetWindow(msg);
        }

        static MessageBoxResult GetWindow(WPFMessageBox msg)
        {
            msg.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            Window? win = null;
            if (Application.Current.Windows.Count > 0)
                win = Application.Current.Windows.OfType<Window>().FirstOrDefault(o => o.IsActive);
            if (win != null)
            {
                var layer = new Grid() { Background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(128, 0, 0, 0)) };
                var original = win.Content as UIElement;
                win.Content = null;
                var container = new Grid();
                container.Children.Add(original);
                container.Children.Add(layer);
                win.Content = container;
                msg.Owner = win;
                msg.ShowDialog();
                container.Children.Clear();
                win.Content = original;
            }
            else
                msg.Show();
            return msg.Result;
        }
    }
}
